#include "UIfile.h"
#include "ui_UIfile.h"
#include "mainwindow.h"
#include <QMenu>
#include <QAction>
#include <QContextMenuEvent>
#include <QMouseEvent>
#include <qinputdialog.h>
#include <QMessageBox>

UIfile::UIfile(QWidget *parent, File *file) :
    QGroupBox(parent),
    ui(new Ui::UIfile)
{
    file_ = file;
    ui->setupUi(this);
    m_parent = static_cast<MainWindow*>(parent);

    // 文件名
    this->setTitle(QString::fromStdString(file_->name_)+"."+QString::fromStdString(file->type_));

    if (file_->IsDir()) {
        switchToDir();
        this->setTitle(QString::fromStdString(file_->name_));
    }

    readOnly = file_->IsReadOnly();

    //创建一个菜单.
    m_contextMenu = new QMenu;
    m_openAction = new QAction("打开",this);
    m_delAction = new QAction("删除",this);
    m_readOnly = new QAction("开启只读",this);
    m_uiattribute = new QAction("属性",this);
    m_moveAction = new QAction("移动",this);
    m_renameAction = new QAction("重命名",this);
    m_editType = new QAction("修改类型",this);
    connect(m_openAction, &QAction::triggered, this, &UIfile::open);
    connect(m_delAction, &QAction::triggered, this, &UIfile::dele);
    connect(m_uiattribute, &QAction::triggered, this, &UIfile::showUiattribute);
    connect(m_readOnly, &QAction::triggered, this, &UIfile::setReadOnly);
    connect(m_moveAction, &QAction::triggered, this, &UIfile::moveFile);
    connect(m_renameAction, &QAction::triggered, this, &UIfile::rename);
    connect(m_editType, &QAction::triggered, this, &UIfile::editType);
    m_contextMenu->addAction(m_openAction);
    m_contextMenu->addAction(m_delAction);
    m_contextMenu->addAction(m_uiattribute);
    m_contextMenu->addAction(m_moveAction);
    m_contextMenu->addAction(m_renameAction);

    if(!isdirectory){
        m_contextMenu->addAction(m_readOnly);
        m_contextMenu->addAction(m_editType);
    }

    if(readOnly)
        m_readOnly->setText("关闭只读");
    else {
        m_readOnly->setText("开启只读");
    }
}

UIfile::~UIfile()
{
    delete ui;
}

//双击事件：打开
void UIfile::mouseDoubleClickEvent(QMouseEvent* event)
{
//    ui->fileName->setText("2");
    UIfile::open();
}

//单击事件
void UIfile::mousePressEvent(QMouseEvent* event)
{
    if(m_parent->Getfile()!=NULL)
        m_parent->Getfile()->setStyleSheet("background-color: white");
    this->setStyleSheet("background-color: #efefef");
    m_parent->Choosefile(this);
    //右键菜单
    if (event->button() == Qt::RightButton)
    {
        m_contextMenu->exec(event->globalPos());
    }
}

//打开
void UIfile::open(){
    UIfile::setTitle(ui->fileName->text());
    if(!UIfile::isdirectory){
        m_parent->file_system_->OpenFile(file_->GetPathName());
        e.setTitle(QString::fromStdString(file_->name_ + "." + file_->type_));
        e.setText(QString::fromStdString(file_->content_));
        string content = file_->content_;
        e.readFile(readOnly);
        e.exec();
        e.setReadOnly(true);
        if (e.getText().toStdString() != content) {
            // 更新文件内容
            file_->content_ = e.getText().toStdString();
            m_parent->file_system_->UpdateFile(*file_);
            file_->ClearCache();
            m_parent->updateFileArea();
        }
    }
    else{
        m_parent->setFileNULL();
        m_parent->setPath(QString::fromStdString(File::GetPathName(file_->path_, file_->name_)));
        m_parent->file_system_->OpenDir(File::GetPathName(file_->path_, file_->name_));
        m_parent->updateFileArea();
    }
}

//删除
void UIfile::dele(){
//    ui->fileName->setText("delete");
    m_parent->muenDelete();
}

//重命名
void UIfile::rename(){
    bool ok;
    QString text;
    QInputDialog input;
    text = input.getText(m_parent, "重命名", "请输入：", QLineEdit::Normal, 0, &ok);
    if(!ok)
        return;
    while (text.toLocal8Bit().length()>3) {
        QMessageBox::warning(m_parent, "警告", "名字超限长", QMessageBox::Yes, QMessageBox::Yes);
        QInputDialog input;
        text = input.getText(m_parent, "重命名", "请输入：", QLineEdit::Normal, 0, &ok);
        }
    if(ok && !text.isEmpty()){
        file_->name_ = text.toStdString();
        m_parent->updateFileArea();
    }
}

void UIfile::showUiattribute(){
    QPixmap p;
    p.load(":/new/prefix1/logo/file-open.png");
    if(!UIfile::isdirectory){
        p.load(":/new/prefix1/logo/text.png");
    }
    m_parent->file_system_->OpenFile(file_->GetPathName());
    a.setAttribute(p, QString::fromStdString(file_->name_), QString::fromStdString(file_->type_),
                   QString::fromStdString(file_->path_),QString::fromStdString(std::to_string(file_->content_.length()) + " 字节"),
                   file_->IsSystemFile(), file_->IsReadOnly());
    a.exec();
}

void UIfile::switchToDir(){
    QPixmap p;
    p.load(":/new/prefix1/logo/file-open.png");
    ui->fileIcon->setPixmap(p);
    isdirectory = true;
}

void UIfile::setTitle(QString s){
    ui->fileName->setText(s);
}

void UIfile::setContent(QString s){
    UIfile::content = s;
}

//设置文件图标大小
void UIfile::setSize(int s){
    if(s == 0){
        ui->fileIcon->setMaximumSize(80,80);
        this->setMaximumSize(80,110);
    }else if(s == 1){
        ui->fileIcon->setMaximumSize(100,100);
        this->setMaximumSize(100,135);
    }else {
        ui->fileIcon->setMaximumSize(150,150);
        this->setMaximumSize(150,180);
    }
}

//设置只读或读写
void UIfile::setReadOnly(){
    file_->SetReadOnly(!readOnly);
    UIfile::readOnly=!readOnly;
    if(readOnly)
        m_readOnly->setText("关闭只读");
    else {
        m_readOnly->setText("开启只读");
    }
}


void UIfile::moveFile() {
    bool ok;
    QString text = "123";
    QInputDialog input;
    text = input.getText(m_parent, "移动", "请输入路径：", QLineEdit::Normal, 0, &ok);
    if(!ok)
        return;
    if (!m_parent->file_system_->ExistDir(text.toStdString())) {
        QMessageBox::warning(m_parent, "警告", "路径不存在", QMessageBox::Yes, QMessageBox::Yes);
        return;
    }
    m_parent->file_system_->MoveFileOrDir(file_, text.toStdString());
//    // 加载内容
//    m_parent->file_system_->LoadFileContent(file_);
//    // 新建文件
//    File file(file_->name_, text.toStdString());
//    file.attribute_ = file_->attribute_;
//    file.type_ = file_->type_;
//    file.content_ = file_->content_;
//    m_parent->file_system_->NewFile(file);
//    if (isdirectory) {
//        // 修改目录项
//        m_
//    }
    
    // 删除原文件
//    dele();
    m_parent->Choosefile(NULL);
    m_parent->updateFileArea();
}

/*修改类型*/
void UIfile::editType(){
    bool ok;
    QString type = "123";
    QInputDialog input;
    type = input.getText(m_parent, " ", "请输入类型：", QLineEdit::Normal, 0, &ok);
    if(!ok)
        return;
    while (type.toLocal8Bit().length()>2) {
        QMessageBox::warning(m_parent, "警告", "类型超限长", QMessageBox::Yes, QMessageBox::Yes);
        QInputDialog input;
        type = input.getText(m_parent, " ", "请输入文件类型：", QLineEdit::Normal, 0, &ok);
    }
    file_->type_ = type.toStdString();
    m_parent->updateFileArea();
}

